package com.zfzs.post.modules.lesson.service.impl;

import com.zfzs.post.modules.goods.domain.Goods;
import com.zfzs.post.modules.lesson.domain.Lesson;
import com.zfzs.post.modules.lesson.repository.LessonRepository;
import com.zfzs.post.modules.lesson.service.LessonService;
import com.zfzs.post.modules.lesson.service.dto.LessonQueryCriteria;
import com.zfzs.post.modules.lesson.service.mapper.LessonMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author wq
 * @date 2019-12-04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class LessonServiceImpl implements LessonService {

    @Autowired
    private LessonRepository lessonRepository;

    @Autowired
    private LessonMapper lessonMapper;

    @Override
    public Object queryAll(LessonQueryCriteria criteria, Pageable pageable) {
        Page<Lesson> page = lessonRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(lessonMapper::toDto));
    }

    @Override
    public Object queryAll(LessonQueryCriteria criteria) {
        return lessonRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
    }

    @Override
    public Lesson findById(Integer lessonId) {
        Optional<Lesson> lesson = lessonRepository.findById(lessonId);
        ValidationUtil.isNull(lesson, "Lesson", "lessonId", lessonId);
        return lesson.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Lesson create(Lesson resources) {
        return lessonRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Lesson resources) {
        Optional<Lesson> optionalLesson = lessonRepository.findById(resources.getLessonId());
        ValidationUtil.isNull(optionalLesson, "Lesson", "id", resources.getLessonId());
        Lesson lesson = optionalLesson.get();
        lesson.copy(resources);
        lessonRepository.save(lesson);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer lessonId) {
        lessonRepository.deleteById(lessonId);
    }

    @Override
    public Lesson findOne(Lesson lesson) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("isDel",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("channelId",ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Lesson> ex = Example.of(lesson, exampleMatcher);

        Optional<Lesson> optional = lessonRepository.findOne(ex);

        return optional.isPresent()?optional.get():null;
    }

    @Override
    public List<Lesson> findList(Lesson lesson) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
                .withMatcher("type", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("isDel",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("channelId",ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Lesson> ex = Example.of(lesson, exampleMatcher);
        Sort sort=new Sort(Sort.Direction.ASC,"createTime");
        List<Lesson> list = lessonRepository.findAll(ex,sort);
        return list;
    }

}